package com.finance.cooperate.feature.base;

import com.finance.cooperate.common.utils.SpringContextUtil;
import com.finance.cooperate.common.utils.StringUtil;
import com.finance.cooperate.feature.core.base.Reader;
import com.finance.cooperate.feature.core.feature.FeatureDefinition;
import com.finance.cooperate.feature.core.service.FeatureOneHotService;
import com.finance.cooperate.feature.service.*;
import org.springframework.context.ApplicationContext;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName ReaderImpl
 * @Description Reader 基础类
 * @Author shen
 * @Date 2022/5/26 11:28
 * @Modify ...
 */
public abstract class ReaderImpl implements Reader {
    // 返回结果
    private Map<String, String> result;

    protected FeatureOneHotService featureOneHotService;
    protected AppService appService;
    protected IdentityService identityService;
    protected PersonalService personalService;
    protected BorrowingService borrowingService;
    protected DeviceService deviceService;
    protected UserService userService;
    protected ScrapyAppsService scrapyAppsService;
    protected CrcScoresService crcScoresService;
    protected CrcConsultasService crcConsultasService;
    protected SmsService smsService;
    protected AppInstallDetailService appInstallDetailService;


    public ReaderImpl() {

        this.result = new HashMap<>();

        ApplicationContext context = SpringContextUtil.getApplicationContext();

        featureOneHotService = (FeatureOneHotService) context.getBean("featureOneHotServiceImpl");
        appService = (AppService) context.getBean("appServiceImpl");
        identityService = (IdentityService) context.getBean("identityServiceImpl");
        personalService = (PersonalService) context.getBean("personalServiceImpl");
        borrowingService = (BorrowingService) context.getBean("borrowingServiceImpl");
        deviceService = (DeviceService) context.getBean("deviceServiceImpl");
        userService = (UserService) context.getBean("userServiceImpl");
        scrapyAppsService = (ScrapyAppsService) context.getBean("scrapyAppsServiceImpl");
        crcScoresService = (CrcScoresService) context.getBean("crcScoresServiceImpl");
        crcConsultasService = (CrcConsultasService) context.getBean("crcConsultasServiceImpl");
        smsService = (SmsService) context.getBean("smsServiceImpl");
        appInstallDetailService = (AppInstallDetailService) context.getBean("appInstallDetailServiceImpl");


    }



    protected void put(FeatureDefinition key, Object value) {
        result.put(key.getValue(), StringUtil.obj2Feature(value));
    }

    protected void putOriginal(FeatureDefinition key, String value) {
        result.put(key.getValue(), value);
    }


    protected void put(String key, Object value) {
        result.put(key, StringUtil.obj2Feature(value));
    }

    protected String get(String key) {
        return result.getOrDefault(key, "");
    }

    protected String getOrDefault(String key, Object defaultValue) {
        return result.getOrDefault(key, String.valueOf(defaultValue));
    }

    protected String get(FeatureDefinition key) {
        return result.getOrDefault(key.getValue(), "");
    }

    protected String getOrDefault(FeatureDefinition key, Object defaultValue) {
        return result.getOrDefault(key.getValue(), String.valueOf(defaultValue));
    }

    protected Map<String, String> getResult() {
        return result;
    }

    public ReaderImpl setResult(Map<String, String> result) {
        this.result = result;
        return this;
    }

    @Override
    public abstract Map<String, String> read(String userId);

}
